<template>
	<view class="content">
		<!-- <view class="tabbar">
			<view class="title">首页</view>
			<uni-icons class="back" type="arrowleft" color="#fff" @click="back"></uni-icons>
			<view class="tabbar-bgc"></view>
		</view> -->
		<uni-nav-bar title="保险金融首页" @clickLeft="goHome" left-icon="left" backgroundColor="#fff" color="#000"
			statusBar="true"/>
		<view class="body">
			<scroll-view class="select-facility-view" @scrolltolower="handleScrolltolowerone()" scroll-y="true"
				:style="{height:setcollapseheight() + 'px'}">
				<!-- 轮播图 -->
				<view class="banner" v-if="swipers.length">
					<swiper circular indicator-dots :autoplay="true">
						<swiper-item @click="toProDetail(item)" v-for="item in swipers">
							<image :src="item.slideshowImgUrl"></image>
						</swiper-item>
					</swiper>
				</view>
				<!-- 产品 -->
				<view class="pro-list">
					<view class="recommend">
						<view class="pro-title">为您推荐</view>
						<!-- <image class="view-item-img" src="@/static/socialized-service/financial-insurance/xin.png"
							mode="heightFix"></image> -->
						<view class="recommend-list">
							<view class="left-page" v-if="recommendList">
								<image class="pro-img" @click="toProDetail(recommendList)" mode="scaleToFill"
									:src="recommendList.content">
								</image>
							</view>
							<view class="right-page">
								<image class="pro-img" v-for="item,index in recommendList2" :key="index"
									@click="toProDetail(item)"
									:src="item.content"
									mode="scaleToFill"></image>
							</view>
						</view>
					</view>
					<view class="pro-title">
						<view class="title">精选产品</view>
						<view class="more" @click="toPorduct">
							更多
							<uni-icons type="arrowright" color="#fff"></uni-icons>
						</view>
						
					</view>
					<view class="selected-pro">
						<view class="selected-card" v-for="item,index in proList" :key="index">
							<view class="left-page">
								<image class="pro-img" :src="item.imgList[0] && item.imgList[0].content"
									mode="scaleToFill"></image>
							</view>
							<view class="right-page">
								<view class="title">{{ item.productTitle }}</view>
								<view class="content" v-html="item.content[0].content"></view>
								<view class="btn">
									<button class="btn-view" @click="toProDetail(item)">查看详情</button>
								</view>
								<!-- <image class="view-item-img"
									src="@/static/socialized-service/financial-insurance/jing.png" mode="heightFix">
								</image> -->
							</view>
						</view>

						<view v-if="proList.length == 0" class="facility-view-nodata">
							<image class="facility-view-blankimg" mode="widthFix" src="@/static/blank.png"></image>
							<view class="facility-view-nodata-line">
								暂无数据
							</view>
						</view>
					</view>
				</view>
			</scroll-view>

		</view>
		<!-- 底部导航栏 -->
		<bottomBar></bottomBar>
	</view>

</template>

<script>
	import bottomBar from "@/subPage1/components/socialized-bottomtabar/financial-bottomtabar"

	import productApi from '@/api/socialized-servce/financial-insurance/product/index.js'
	export default {
		components: {
			bottomBar,
		},
		data() {
			return {
				// 轮播图
				swipers: [],
				// 推荐
				recommendList: {},
				recommendList2: [],
				// 精选
				proList: [],
				proPage: {
					page: 1,
					size: 5,
					total: 0,
				},
				provinceId: '',
				cityId: '',
				areaId: ''
			}
		},
		onLoad() {
			this.provinceId = uni.getStorageSync('provinceId')
			this.cityId = uni.getStorageSync('cityId')
			this.areaId = uni.getStorageSync('areaId')
			this.getSlideList()
			this.getRecList()
			this.getProList()
		},
		onShow() {

		},
		methods: {
			toPorduct() {
				this.$togo('/subPage1/socialized-service/financial-insurance/product/product')
			},
			// 上拉加载更多
			handleScrolltolowerone() {
				let len = this.proPage.page * this.proPage.size
				if (len >= this.proPage.total) return
				this.proPage.page += 1
				this.loadDataList()
			},
			updataContent() {
				this.proPage.page = 1
				this.getProList()
				this.getSlideList()
			},
			// 获取轮播图数据
			getSlideList() {
				productApi.queryByConditionSlide({
					page: 1,
					size: 100,
					condition: {
						provinceId: this.provinceId,
						cityId: this.cityId,
						areaId: this.areaId,
						serviceProviderNo: uni.getStorageSync('serviceProviderNo'),
					}
				}).then(res => {
					this.swipers = res.data.content
				})
			},
			// 获取推荐产品数据
			getRecList() {
				productApi.queryPageByCondition({
					page: 1,
					size: 3,
					condition: {
						recommendFlag: true,
						provinceId: this.provinceId,
						cityId: this.cityId,
						areaId: this.areaId,
						serviceProviderNo: uni.getStorageSync('serviceProviderNo'),
					},
			}).then(res => {
			    let list = res.data.content;
			    let processProduct = (product) => {
			        let imageItem = this.sliceStr(product.productContent).filter(el => el.type == 'image')[0] || {};
			        imageItem['productId'] = product.productId;
			        return imageItem;
			    };
			
			    this.recommendList = processProduct(list[0]);
			    
			    this.recommendList2 = list.slice(1, 3).map(product => {
			        return processProduct(product);
			    }).filter(product => product.hasOwnProperty('productId'));
			})
			},
			// 拆
			sliceStr(str) {
			  let result = []
			  const reg = /<img.+?src=[\"\'](.+?)[\"\'].{0,}?>/g;
			  let index = 0
			  str = str.replace(/<p/g,'<p class="nodeText"')
			  str.replace(reg,(value,word,position)=>{
			    const valueLen = value.length
			    const text = {
			      type: 'text',
			      content: str.slice(index,position)
			    }
			    const img = {
			      type: 'image',
			      content: word
			    }
			    result.push(text,img)
			    index = position +valueLen
			  })
			  result.push({
			    type: 'text',
			    content: str.slice(index)
			  })
			  return result
			},
			// 获取精选产品数据
			getProList() {
				productApi.queryPageByCondition({
					...this.proPage,
					condition: {
						financialTitle: '',
						choicenessFlag: true,
						provinceId: this.provinceId,
						cityId: this.cityId,
						areaId: this.areaId,
						serviceProviderNo: uni.getStorageSync('serviceProviderNo'),
					}
				}).then(res => {
					let list = res.data.content
					this.proPage.total = res.data.totalElements
					list.forEach(el=>{
						let arr = this.sliceStr(el.productContent).filter(el=>el.type === 'image')
						let content = this.sliceStr(el.productContent).filter(el=>el.type === 'text')
						el['imgList'] = [...arr]
						el['content'] = content
					
					})
					if (this.proPage.page == 1) {
						this.proList = list
					} else {
						this.proList.push(...list) 
					}
				})
			},

			// 跳转到详情
			toProDetail(row) {
				const url = '/subPage1/socialized-service/financial-insurance/product/product-detail/index'
				this.$togo(url, {
					productId: row.productId,
					productUserId: row.productUserId
				})
			},

			//  设置可选择设施高度
			setcollapseheight() {
				let winHeight = 0
				uni.getSystemInfo({
					success: function(res) {
						winHeight = res.windowHeight
					}
				})
				let oldHeight = uni.upx2px(230)
				winHeight = parseInt(winHeight) - oldHeight
				// #ifndef H5
				winHeight = winHeight - 10
				// #endif
				return winHeight
			},
			//返回上一步
			back() {
				uni.navigateBack({ delta: 1 })
			},
			goHome() {
				uni.switchTab({
					url: '/pages/socialized-service/selection-system/selection-system'
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.content {

		// .body {
		// 	margin-top: -360rpx;
		// }

		.tabbar {
			width: 100%;
			height: 516rpx;
			background: linear-gradient(315deg, #0480FB 0%, #57DED9 100%);
			color: #393939;
			display: flex;
			justify-content: center;
			// align-content: center;
			flex-wrap: wrap;
			position: relative;

			.title {
				// width: 750rpx;
				margin-top: 90rpx;
				// margin-top: 40rpx;
				height: 32rpx;
				line-height: 32rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #FFFFFF;
			}

			.back::before {
				font-size: 48rpx;
				position: absolute;
				left: 20rpx;
				top: 85rpx;
			}
			
			.tabbar-bgc {
				width: 750rpx;
				height: 272rpx;
				background: linear-gradient(360deg, #F5F6FA 0%, rgba(245, 246, 250, 0) 100%);
				position: absolute;
				bottom: 0rpx;
			}
		}

		.banner {
			// margin-top: -360rpx;
			width: 100%;
			margin-bottom: 30rpx;
			display: flex;
			justify-content: center;
			align-self: center;
			z-index: 2;
			// padding: 0 30rpx;

			swiper {
				z-index: 2;
				width: 100%;
				height: 396rpx;
				// border-radius: 20rpx;
				overflow: hidden;

				image {
					z-index: 2;
					width: 100%;
					height: 100%;
					// border-radius: 20rpx;
					box-sizing: border-box;
				}
			}

		}

		.pro-list {
			width: 100%;
			padding: 0 30rpx;
			box-sizing: border-box;

			.pro-title {
				width: 100%;
				height: 88rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #000;
				line-height: 88rpx;
				position: relative;
				
				.title {
					padding: 0 10rpx;
					display: inline-block;
					height: 46rpx;
					line-height: 46rpx;
					// border-bottom: 1px solid #f6d379;
				}
				.more {
					position: absolute;
					right: -60rpx;
					top: 22rpx;
					background: linear-gradient(to right, #f7b228, #ff8b45);
					color: #fff;
					height: 46rpx;
					line-height: 46rpx;
					padding: 0 46rpx 0 30rpx;
					border-radius: 24rpx;
					
					.uni-icons {
						color: #fff;
						
						&::before {
							color: #fff;
						}
					}
				}
			}

			.recommend {
				background-image: linear-gradient(135deg, #88adff, #2065ff);
				border-radius: 12rpx;
				padding: 0 30rpx 30rpx;
				position: relative;
				border-top: 4rpx solid #8dc5ff;

				.pro-title {
					width: 100%;
					height: 88rpx;
					font-size: 28rpx;
					font-weight: 400;
					color: #fff;
					line-height: 88rpx;
				}

				.view-item-img {
					position: absolute;
					width: 50rpx;
					height: 50rpx;
					right: 50rpx;
					top: -4rpx;
				}

				.recommend-list {
					height: 400rpx;
					display: flex;
					font-size: 0;

					.left-page {
						width: 70%;
						height: 100%;
						margin-right: 16rpx;

						.pro-img {
							width: 100%;
							height: 100%;
							border: 2px solid #fff;
							border-radius: 20rpx;
						}
					}

					.right-page {
						width: 30%;
						height: 100%;

						.pro-img {
							width: 100%;
							height: calc(50% - 8rpx);
							border: 2px solid #fff;
							border-radius: 20rpx;

							&:first-child {
								margin-bottom: 6rpx;
							}
						}
					}
				}
			}

			.selected-pro {

				.selected-card {
					background-color: #fff;
					border-radius: 12rpx;
					padding: 30rpx 30rpx 30rpx 0;
					position: relative;
					// border-top: 4rpx solid #8dc5ff;
					display: flex;
					margin-bottom: 30rpx;

					.left-page {
						width: 200rpx;
						height: 170rpx;
						margin-right: 20rpx;
						padding-left: 30rpx;
						border-left: 6rpx solid #2262f3;

						.pro-img {
							width: 100%;
							height: 100%;
						}
					}

					.right-page {
						width: calc(100% - 260rpx);

						.title {
							font-size: 40rpx;
							margin-bottom: 10rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.content {
							height: 39rpx;
							margin-bottom: 12rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							::v-deep* {
								font-size: 28rpx!important;
								overflow: hidden;
								text-overflow: ellipsis;
								word-spacing: nowrap;
							}
						}

						.btn {
							.btn-view {
								width: 180rpx;
								font-size: 24rpx;
								float: right;
								color: #ad9a7b;
								background-color: #fff;
								border: 2rpx solid #ad9a7b;
								border-radius: 40rpx;
							}
						}

						.view-item-img {
							position: absolute;
							width: 50rpx;
							height: 50rpx;
							right: 50rpx;
							top: -4rpx;
						}
					}
				}
			}
		}
	}
</style>